﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VgoCardChange.Models;
using VgoCardChange.Models.Objects;

namespace VgoCardChange.Controllers
{
    public class ItemRelayController : Controller
    {
        //
        // GET: /ItemRelay/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult ItemRelay()
        {
            return View();
        }
        public ActionResult CreateItemRelay()
        {
            
            ItemRelayModels IRelayMd = new ItemRelayModels();
            ServerModels SvMd = new ServerModels();
            GuildModels GuildMd = new GuildModels();
            ViewData["listuser"] = new SelectList(IRelayMd.GetUser(), "UserID", "UserName");
            ViewData["listitem"] = new SelectList(IRelayMd.GetItem(), "ItemID", "ItemName");
            ViewData["listserver"] = new SelectList(SvMd.GetServer(), "ServerID", "ServerName");
            ViewData["listguild"] = new SelectList(GuildMd.GetGuild(), "GuildID", "GuildName");
            return View();
        }
        [HttpPost]
        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult CreateItemRelay(int listitem, int Quantity, string IsBind, int listuser, int listserver, int listguild, string mailTo, string subject, string content)
        {

            if (!ModelState.IsValid)

                return View();

            try
            {
                ItemRelayModels IRelayMd = new ItemRelayModels();
                ItemRelay ItRelay = new ItemRelay();
                ItRelay.ItemID = listitem;
                ItRelay.Quantity = Quantity;
                ItRelay.IsBind = IsBind;
                ItRelay.UserID = listuser;
                ItRelay.ServerID = listserver;
                ItRelay.GuildID = listguild;
                IRelayMd.CreateItemRelay(ItRelay);
                string mailFrom = "h.thuhy@gmail.com";
                string pass = "nguyenthithuit";
                string port = "587";
                string host = "smtp.gmail.com";
                //  bool enableSsl = true;
                IRelayMd.SendMailFull(mailFrom, pass, host, port, mailTo, subject, content, true);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        public string GetEmail(int listuser)
        {
            ItemRelayModels ItemRelayMd = new ItemRelayModels();
            string email = ItemRelayMd.GetEmail(listuser);
            return email;
        }
    }
}
